package controllers;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import admin.models.MetaSeoModel;

import systems.ConfigSystem;

import entities.GoogleAdsense;
import entities.MetaSeo;
import entities.SiteConfig;

import models.GoogleAdsenceModel;

public class AdsenseServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		SiteConfig config = ConfigSystem.getConfigs();
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		if (id == null || id.isEmpty()) {
			response.sendRedirect("/msg/page-not-found");
			return ;
		}
		
		GoogleAdsenceModel googleAdsenseModel = GoogleAdsenceModel.getInstance();
		GoogleAdsense googleAdsense = googleAdsenseModel.get(id);
		
		if (googleAdsense == null) {
			response.sendRedirect("/msg/page-not-found");
			return ;
		}
		
		//Set seo content
		MetaSeoModel metaseoModel = MetaSeoModel.getInstance();
		MetaSeo metaseo = metaseoModel.get("adsense");
		
		//Cache header
		response.addHeader("Cache-Control", "public");
		response.addHeader("Cache-Control", "max-age=" + 60000);
		
		request.setAttribute("googleAdsense", googleAdsense);
		request.setAttribute("metaseo", metaseo);
		try {
			request.getRequestDispatcher("/templates/" + config.getTemplate() + "/pages/adsense.jsp").forward(request, response);
		} catch (ServletException e) {
			
		}
	}
}
